// pages/MyBalanceDetailPage/MyBalanceDetailPage.js
var screenUtil = require("../../utils/screen.js");
var api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leaderId:'',
    balanceList:[],
    resultList:[]
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    that.data.leaderId = options.leaderId
    var screen = screenUtil.screenUtil()
    var scrHeight = screen.windowHeight
    that.setData({
      navheight:getApp().globalData.zHeight*2,
      arr:that.data.arr,
      isTriggered:false,
      scrHeight:scrHeight,
      hideModal:false
    })

    
    api.request1({"leaderId":that.data.leaderId,"path": "user/balanceList","token":wx.getStorageSync('token')}, function (result) {
      if(result.code=='0'){
        that.data.balanceList = result.data;
        that.dealBalanceList();
        that.setData({
          scoreList:result.data,
          noData:false
        })
      }else{
        that.setData({
          noData:true
        })
      }  
    }, function (error) {
      console.log('error==')
    })


  },
  handlerGobackClick() {
    wx.navigateBack({
      delta: 1
    });
  },
  handleRefresher(event){
    // 发送请求获取最新的视频数据
    var that = this;
    api.request1({"leaderId":that.data.leaderId,"path": "user/balanceList","token":wx.getStorageSync('token')}, function (result) {
      if(result.code=='0'){
        that.setData({
          scoreList:result.data,
          isTriggered:false,
        })
      }  
    }, function (error) {
      console.log('error==')
    })
  },
  dealBalanceList(){
    var that = this
    var month = [];
    for(let i=0;i<that.data.balanceList.length;i++){
      var item = that.data.balanceList[i];
      // console.log('ff==',item['time'].split(' ')[0].split('-')[0]+'-'+item['time'].split(' ')[0].split('-')[1]);
      month.push(item['time'].split(' ')[0].split('-')[0]+'-'+item['time'].split(' ')[0].split('-')[1])

    }
    // console.log(month)
    let newList = Array.from(new Set(month))
    // console.log('newList', newList);

    var reslutArr=[];
    for(let i=0;i<newList.length;i++){
      var item = {};
      item.date = newList[i].split('-')[0]+'年'+newList[i].split('-')[1]+'月';
      var arr = [];
      var chong = 0;
      var jiangli = 0
      var xiaofei = 0
      var tuikuan = 0
      for(let j=0;j<that.data.balanceList.length;j++){
        var item1 = that.data.balanceList[j];
        if(item1['time'].indexOf(newList[i])!=-1){
          arr.push(item1)
          if(item1['type']=='充值'){
            chong=chong+item1['money']
          }else if(item1['type']=='奖励'){
            jiangli=jiangli+item1['money']
          }else if(item1['type']=='消费'){
            xiaofei=xiaofei+item1['money']
          }else if(item1['type']=='退款'){
            tuikuan=tuikuan+item1['money']
          }
        }
      }
      item.chong = chong;
      item.jiangli = jiangli;
      item.xiaofei = xiaofei;
      item.tuikuan = tuikuan;
      item.arrayList = arr;
      item.kai = true;
      reslutArr.push(item);
    }
    that.data.resultList = reslutArr
    that.setData({
      reslutArr:reslutArr,
      hideModal:false,
      noData:reslutArr.length==0?true:false
    })
    console.log('resultArr=',reslutArr)
  },
  selectTypeClick(){
    var that = this;
    that.setData({
      showSelectType:true,
      isIphoneX: getApp().globalData.statusBarHeight>=44?true:false,
      hideModal:true
    })
  },
  hideModal(){
    var that = this;
    that.setData({
      hideModal:false
    })
  },
  showChargeDetail:function(e){
    var index = e.currentTarget.dataset.id;
    console.log('eee=',index)
    wx.navigateTo({
      url: '/pages/RechargeDetailsPage/RechargeDetailsPage'
    });
  },
  selectAll(){
    var that = this
    that.dealBalanceList()
  },
  selectType:function(e){
    var that = this
    var typeArr = ['充值','奖励','消费','退款','转让','清零'];
    var index = e.currentTarget.dataset.id;
    console.log(index);

    var selectArr= []
    for(let i=0;i<that.data.balanceList.length;i++){
      var item = that.data.balanceList[i];
      if(item.type==typeArr[index]){
        selectArr.push(item)
      }
    }

    var month = [];
    for(let i=0;i<selectArr.length;i++){
      var item = selectArr[i];
      // console.log('ff==',item['time'].split(' ')[0].split('-')[0]+'-'+item['time'].split(' ')[0].split('-')[1]);
      month.push(item['time'].split(' ')[0].split('-')[0]+'-'+item['time'].split(' ')[0].split('-')[1])

    }
    // console.log(month)
    let newList = Array.from(new Set(month))
    // console.log('newList', newList);

    var reslutArr=[];
    for(let i=0;i<newList.length;i++){
      var item = {};
      item.date = newList[i].split('-')[0]+'年'+newList[i].split('-')[1]+'月';
      var arr = [];
      var chong = 0;
      var jiangli = 0
      var xiaofei = 0
      var tuikuan = 0
      for(let j=0;j<selectArr.length;j++){
        var item1 = selectArr[j];
        if(item1['time'].indexOf(newList[i])!=-1){
          arr.push(item1)
          if(item1['type']=='充值'){
            chong=chong+item1['money']
          }else if(item1['type']=='奖励'){
            jiangli=jiangli+item1['money']
          }else if(item1['type']=='消费'){
            xiaofei=xiaofei+item1['money']
          }else if(item1['type']=='退款'){
            tuikuan=tuikuan+item1['money']
          }
        }
      }
      item.chong = chong;
      item.jiangli = jiangli;
      item.xiaofei = xiaofei;
      item.tuikuan = tuikuan;
      item.arrayList = arr;
      item.kai = true;
      reslutArr.push(item);
    }
    that.data.resultList = reslutArr
    that.setData({
      reslutArr:reslutArr,
      hideModal:false,
      noData:reslutArr.length==0?true:false
    })

  },
  optionClick:function(e){
    var that = this
    var index = e.currentTarget.dataset.id;
    var item = that.data.resultList[index];
    item.kai = !item.kai;
    console.log('dd==',index)
    that.setData({
      reslutArr:that.data.resultList,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})